package com.lebang.service.cences;

import com.lebang.domain.CencesInvite;
import com.lebang.example.CencesInviteExample;
import com.lebang.mapper.CencesInviteMapper;
import com.lebang.mapper.InviteReleaseMapper;
import com.lebang.mapper.NewInviteTaskRecordMapper;
import com.lebang.mapper.NewMemberInviteDetailMapper;
import com.lebang.model.MyConfig;
import com.lebang.model.cences.CencesMsg;
import com.lebang.model.cences.InviteTaskModel;
import com.lebang.model.enums.InviteEnum;
import com.lebang.service.BaseService;
import com.lebang.service.mongodb.CencesMsgMongodb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 师徒统计
 *
 * @Author xwj
 * @Date 2020-04-09
 */
@Service
public class CencesInviteService extends BaseService<CencesInvite, Integer, CencesInviteExample> {
    @Autowired
    private CencesInviteMapper baseMapper;

    @Autowired
    private NewMemberInviteDetailMapper newMemberInviteDetailMapper;

    @Autowired
    private NewInviteTaskRecordMapper newInviteTaskRecordMapper;

    @Autowired
    private InviteReleaseMapper inviteReleaseMapper;

    @Autowired
    private CencesMsgMongodb cencesMsgMongodb;



    public void starDataInit(LocalDateTime end) {
        this.starDataInit(end, null);
    }

    /**
     * 统计指定日期的渠道效果
     *
     * @param end   结束日期
     * @param brand 渠道
     */
    public void starDataInit(LocalDateTime end, List<String> brand) {
        long starTime = System.currentTimeMillis();
        //star 开始日期  end 结束日期
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        int time = Integer.parseInt(end.toLocalDate().minusDays(1).format(df));

        CencesInvite vo = new CencesInvite(time, MyConfig.getEnv());

        LocalDateTime star = end.minusDays(1);
        //今日收徒数
        int count = inviteReleaseMapper.countTodayNum(star,end);
        vo.setInviteNum(count);
        //有效徒弟数
        int effect = newMemberInviteDetailMapper.countTodayNum(star,end);
        vo.setValidNum(effect);
        //徒弟任务完成
        List<InviteTaskModel> inviteTaskModels = newInviteTaskRecordMapper.selectTodayTask(star,end);
        long allAward = 0L;

        for(InviteTaskModel s: inviteTaskModels) {
            if (s.getCategory() == InviteEnum.NEW_TASK.getIndex()) {
                //新手任务阶段任务
                vo.setNewTaskAward(s.getAward());
                vo.setNewTaskFinish(s.getNum());
                allAward= allAward+ s.getAward();
            }
            if (s.getCategory() == InviteEnum.FIRST_CASH.getIndex()) {
                //首次提现
                vo.setFirstCashAward(s.getAward());
                vo.setFinishNum(s.getNum());
                allAward= allAward+ s.getAward();
            }
        }
        vo.setAllAward(allAward);
        baseMapper.insertAll(vo);


        cencesMsgMongodb.save(new CencesMsg("每日师徒邀请统计", (System.currentTimeMillis() - starTime)));

    }


    public void deleteByTime(Integer time) {
        CencesInviteExample cencesInviteExample = new CencesInviteExample();
        cencesInviteExample.createCriteria().andTimeEqualTo(time).andAppIdEqualTo(MyConfig.getEnv());
        baseMapper.deleteByExample(cencesInviteExample);
    }
}
